/*
 * Copyright (C) 2011 by ayoung <ayoung.p@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.peaceb.ruletherain;

import java.io.File;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;

public class UserDatabaseHelper extends DatabaseHelper {
    private static final int    DB_VERSION   = 2;
    private static final String USER_DB_PATH = "/ruletherain/user.db";
    private static final String USER_DB_DIR  = "/ruletherain";
    
    public  static final String TABLE_NAME = "user_location";
    public  static final String ITEM_ID        = "_id";
    public  static final String ITEM_CITY_NAME = "city_name";
    public  static final String ITEM_LATITUDE  = "latitude";
    public  static final String ITEM_LONGITUDE = "longitude";
    public  static final String ITEM_ALTITUDE  = "altitude";

    public UserDatabaseHelper(Context context, String name) {
        super(context, name, null, DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + TABLE_NAME + "(" + 
                ITEM_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                ITEM_CITY_NAME + " TEXT, " +
                ITEM_LATITUDE + " REAL, " +
                ITEM_LONGITUDE + " REAL, " +
                ITEM_ALTITUDE + " REAL);");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }
    
    /**
     * Get user database path
     * 
     * @return
     */
    public static String getDatabasePath() {
        if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
            Trace.w("sd card is not mounted");
            return null;
        }
        
        String filePath = Environment.getExternalStorageDirectory() + USER_DB_PATH;
        
        return filePath;
    }
    
    /**
     * Check database store directory
     * 
     * @return
     */
    public static int checkOrCreateDir() {
        String dirPath = Environment.getExternalStorageDirectory() + USER_DB_DIR;
        File dir = new File(dirPath);
        if (dir.exists()) {
            return C.SUCCESS;
        }
        else {
            if (dir.mkdir()) {
                return C.SUCCESS;
            }
            else {
                return C.ERROR;
            }
        }
    }
    
    /**
     * Get modified time for database file
     * 
     * @return
     */
    public static long getModifiedTime() {
        long modTime = 0;
        
        String dbPath = getDatabasePath();
        if (dbPath == null) {
            return 0;
        }
        
        try {
            File file = new File(getDatabasePath());
            modTime = file.lastModified();
        }
        catch (NullPointerException e) {
            Trace.w(e.toString());
            return 0;
        }
        
        return modTime;
    }
}
